Update and Add Data

Add New Record

Description
This example demonstrates how to write a reusable function that programmatically constructs a database record and inserts the new record into the database. The reusable function can be called from any page.
Variables
RecordControl
Select a record control
Table Name
Select the name of the database table
Field Name
Select a field in the table
Applies to
RecordClass class
Code
 
/// 
/// This method inserts a new record into the table.
/// Start a transaction before calling this function.
/// 
public static void AddRecord() 
{ 
    // Set up the record object.
    ${${Table Name}RecordClassName} rec = new ${${Table Name}RecordClassName}(); 

    //Set the fields of the new record as follows.
    rec.${Field Name} = "1";

    // Set values of other required fields here.
    // Save the new record.
    // Save adds the record to the database but commit 
    // happens when the page has completed its processing
    rec.Save(); 

    //Example shown below calls AddRecord() function on a button click.
    //Note that you need to add transaction handling as shown below.
    // public override void Button_Click( object sender, EventArgs args)
    // {
    //    try
    //    {
    //        DbUtils.StartTransaction();
    //        OrdersRecord.AddRecord();
    //        DbUtils.CommitTransaction();
    //        this.DataChanged = true;
    //    }
    //    catch(Exception ex)
    //    {
    //        DbUtils.RollBackTransaction();
    //        BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "UNIQUE_SCRIPTKEY", ex.Message);
    //    }
    //    finally
    //    {
    //        DbUtils.EndTransaction();
    //    }
    // }    
}
     

Terms of Service Privacy Statement